﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_Raovat_ContentEditor
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)

#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            Dim curId As Integer = IIf(Page.RouteData.Values("Id") Is Nothing, 0, Page.RouteData.Values("Id"))
            If curId <> 0 Then
                LoadListStatus(curId)
            End If
            'active tab tuong ung voi Page
            'InVisble button ko can thiet
            If Request.PhysicalPath.Contains("my-oto.html") Then
                lbtList.CssClass = "current_manage"
                lbtList.Text = "<span class='current_manage'>Danh sách</span>"
                lbtEdit.Visible = False
                lbtView.Visible = False
                btnDelete.Visible = False
                tabs_state.Visible = False
                cboAction.Visible = False
                'Permission
                '  lbtAdd.Visible = HasPermission(Function_Name.Raovat, Session("RoleID"), 0, Audit_Type.Create)
            ElseIf Request.PhysicalPath.Contains("dang-tin-ban-oto.html") Or Request.PhysicalPath.Contains("Step1.aspx") Or Request.PhysicalPath.Contains("Step2.aspx") Or Request.PhysicalPath.Contains("Step3.aspx") Or Request.PhysicalPath.Contains("Step4.aspx") Then
                lbtAdd.CssClass = "current_manage"
                lbtAdd.Text = "<span class='current_manage'>Đăng tin</span>"
                lbtEdit.Visible = False
                lbtView.Visible = False
                btnDelete.Visible = False
                tabs_state.Visible = False
                cboAction.Visible = False
                'Permission
                '  lbtAdd.Visible = HasPermission(Function_Name.Raovat, Session("RoleID"), 0, Audit_Type.Create)
            ElseIf Request.PhysicalPath.Contains("chi-tiet-oto.html") Then
                lbtView.CssClass = "current_manage"
                lbtView.Text = "<span class='current_manage'>Xem</span>"
                'Permission
                ' setPermission()
            ElseIf Request.PhysicalPath.Contains("chinh-sua-oto.html") Then
                lbtEdit.CssClass = "current_manage"
                lbtEdit.Text = "<span class='current_manage'>Sửa tin</span>"
                'Permission
                'setPermission()
            End If
            If Not Page.RouteData.Values("Id") Is Nothing Then hidID.Value = Page.RouteData.Values("Id") 'Load list action status    Dim ArrId As String() = hidID.Value.Split(",")    If (ArrId.Length <= 1) Then        LoadListStatus(hidID.Value)    End If    'Permission    btnDelete.Visible = HasPermission(Function_Name.Raovat, Session("RoleID"), 0, Audit_Type.Delete)
            btnDelete.Attributes.Add("onclick", "return ComfirmDialog('Bạn có chắc chắn muốn xóa không?',0,'" + btnDelete.ClientID + "','" + hidID.Value.ToString + "',1);")
        End If
    End Sub
    Protected Sub setPermission()
        'lbtAdd.Visible = HasPermission(Function_Name.Raovat, Session("RoleID"), 0, Audit_Type.Create)
        'lbtView.Visible = HasPermission(Function_Name.Raovat, Session("RoleID"), 0, Audit_Type.ViewContent)
        'lbtEdit.Visible = HasPermission(Function_Name.Raovat, Session("RoleID"), 0, Audit_Type.Edit)
        'btnDelete.Visible = HasPermission(Function_Name.Raovat, Session("RoleID"), 0, Audit_Type.Delete)
    End Sub
    'Load list action status of current status
    Sub LoadListStatus(ByVal iRaovatId As Integer)
        Using Data As New VSEntities
            'Get Current StatusId of object
            Dim cst = (From q In Data.RaoVats Where q.RaovatId = iRaovatId Select q).SingleOrDefault
            Dim iCurrentStatusId As Byte = cst.StatusId
            'Load action tuong ung voi current statusid
            Dim st = (From q In Data.StatusActions Where q.CurrentStatusId = iCurrentStatusId Select q Order By
                     q.NextStatusId).ToList()
            For Each i In st
                ' If HasPermission(Function_Name.Raovat, Session("RoleID"), i.CurrentStatusId, i.Audit) Then
                Dim _item As New ListItem(i.Title, i.NextStatusId)
                cboAction.Items.Insert(0, _item)
                'End If
            Next
            Dim iTem As New ListItem(cst.Status.Title, iCurrentStatusId)
            cboAction.Items.Insert(0, iTem)
        End Using
    End Sub
    Protected Sub lbtList_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtList.Click
        Response.Redirect("my-oto.html")
    End Sub
    Protected Sub lbtAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtAdd.Click
        Response.Redirect("dang-tin-ban-oto.html")
    End Sub
    Protected Sub lbtEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtEdit.Click
        Response.Redirect("chinh-sua-oto.html?RaovatId=" & hidID.Value & "")
    End Sub
    Protected Sub lbtView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtView.Click
        Response.Redirect("xem-oto-" & hidID.Value & ".html")


    End Sub
    Protected Sub lnkbtnDelete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDelete.Click
        Dim intId As Integer
        Dim strLogName As String = ""
        Using data As New VSEntities
            intId = hidID.Value
            Dim l = (From d In data.ThongSoCoBans Where d.RaoVatId = intId Select d).FirstOrDefault
            If Not l Is Nothing Then
                data.ThongSoCoBans.DeleteObject(l)
            End If


            Dim g = (From k In data.ThongSoKyThuats Where k.RaoVatId = intId Select k).FirstOrDefault
            If Not g Is Nothing Then
                data.ThongSoKyThuats.DeleteObject(g)
            End If


            Dim u = (From r In data.TienNghis Where r.RoaVatId = intId Select r).FirstOrDefault
            If Not u Is Nothing Then
                data.TienNghis.DeleteObject(u)

            End If

            Dim t = (From c In data.AnToans Where c.RaoVatId = intId Select c).FirstOrDefault
            If Not t Is Nothing Then
                data.AnToans.DeleteObject(t)
            End If


            Dim a = (From b In data.RaovaImages Where b.RaoVatId = intId Select b).ToList
            For Each abc In a
                data.RaovaImages.DeleteObject(abc)
            Next

            Dim j = (From s In data.TinNhans Where s.RaoVatId = intId Select s).ToList
            For Each o In j
                data.TinNhans.DeleteObject(o)
            Next

            Dim w = (From z In data.TinDaLuus Where z.RaoVatID = intId Select z).ToList
            For Each m In w
                data.TinDaLuus.DeleteObject(m)
            Next

            data.SaveChanges()
            Dim q = (From p In data.RaoVats Where p.RaovatId = intId Select p).FirstOrDefault
            Try
                data.RaoVats.DeleteObject(q)
                Data.SaveChanges()
                Excute_Javascript("Alertbox('Xóa dữ liệu thành công.');window.location ='my-oto.html';", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Xóa thất bại.');", Me.Page, True)
            End Try
        End Using
    End Sub
    Protected Sub cboAction_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboAction.SelectedIndexChanged
        'Nếu trạng thái đang chọn khác với trạng thái ban đầu thì mới đi chuyển trạng thái
        If (cboAction.SelectedValue <> cboAction.Items(0).Value) Then
            Response.Redirect("Action.aspx?RaovatId=" & hidID.Value & "&CStatusId=" & cboAction.Items(0).Value & "&NStatusId=" & cboAction.SelectedValue)
        End If
    End Sub
#End Region

End Class
